/** 
* 文 件 名:CommonUtil.java
* Copyright (C) 2007-2012:杭州希禾信息技术有限公司 版权所有 
* 创 建 人:胡浩
* 日    期:2012-3-19 下午9:12:35
* 修 改 人: 
* 日   期:
* 修改描述:
* 描   述:TODO
*/

package net.seehealth.healthpad.utils;

import java.util.Collection;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;

/**
 * @className CommonUtil
 * @author 胡浩
 * @description 常用工具类
 * @date 2012-3-19 下午9:12:35
 */
public class CommonUtils {

	/**
	 * 判断对象是否为空
	 * @param object 当前对象
	 * @return true:为空 false:不为空
	 */
	public static boolean isEmpty(Object object) {
		if (object == null)
			return true;
		if (object instanceof CharSequence)
			return ((CharSequence) object).length() == 0;
		if (object instanceof Collection)
			return ((Collection<?>) object).isEmpty();
		if (object instanceof Map)
			return ((Map<?,?>) object).isEmpty();
		if (object.getClass().isArray())
			return java.lang.reflect.Array.getLength(object) == 0;
		return false;
	}

	/**
	 * 判断对象是否不为空
	 * @param object 当前对象
	 * @return true:不为空  false:为空
	 */
	public static boolean isNotEmpty(Object object) {
		return !isEmpty(object);
	}
	
	/**
	 * 发送Email
	 * @param context 当前上下文
	 * @param email	email地址
	 */
	public static void sendEmail(Context context, String email){
		Uri uri = Uri.parse("mailto:" + email);
		Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
		context.startActivity(intent);
	}
	
	/**
	 * 得到一个指定大小的 Bitmap类型的缩略图
	 * @param bitmap
	 * @param x
	 * @param y
	 * @param roundPx
	 * @return
	 */
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float x,
			float y, int roundPx) {
		if (CommonUtils.isEmpty(bitmap))
			return null;
		return RoundedCornerBitmap(getBitmap(bitmap, x, y), roundPx);
	}

	/**
	 * 得到指定大小的Bitmap
	 * @param bitmap 目标Bitmap
	 * @param x 宽度
	 * @param y 高度
	 * @return 指定大小的Bitmap
	 */
	public static Bitmap getBitmap(Bitmap bitmap, float x, float y) {
		int bmpWidth = bitmap.getWidth();
		int bmpHeight = bitmap.getHeight();
		float scalex_x = (float) x / bmpWidth;
		float scalex_y = (float) y / bmpHeight;
		Matrix matrix = new Matrix();
		matrix.postScale(scalex_x, scalex_y);
		return Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix,
				true);
	}

	public static Bitmap RoundedCornerBitmap(Bitmap bitmap, int roundPx) {
		Bitmap bmap = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bmap);
		canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG
				| Paint.FILTER_BITMAP_FLAG));
		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bmap.getHeight(), bmap.getWidth());
		final RectF rectF = new RectF(rect);
		paint.setAntiAlias(true);
		paint.setFlags(Paint.ANTI_ALIAS_FLAG);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return bmap;
	}

}
